package cn.db.tools.dbtools.controller;

import cn.db.tools.dbtools.message.ToastMessage;
import com.google.common.hash.Hashing;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import org.pomo.toasterfx.model.impl.ToastTypes;

import java.nio.charset.StandardCharsets;
import java.util.Locale;

/**
 * @author：zooooooooy
 * @date: 2023/8/9 - 18:10
 */
public class Md5Controller {

    @FXML
    private TextArea originalTextArea;

    @FXML
    TextField textField32Upper;

    @FXML
    TextField textField32Down;

    @FXML
    protected void originTextChanged() {

        textField32Upper.setText(Hashing.md5().hashBytes(originalTextArea.getText().getBytes(StandardCharsets.UTF_8)).toString().toUpperCase(Locale.ROOT));
        textField32Down.setText(Hashing.md5().hashBytes(originalTextArea.getText().getBytes(StandardCharsets.UTF_8)).toString());
    }

    @FXML
    protected void copyTextFieldUpper() {
        Clipboard sc = Clipboard.getSystemClipboard();
        ClipboardContent clipboardContent = new ClipboardContent();
        clipboardContent.put(DataFormat.PLAIN_TEXT, textField32Upper.getText());

        sc.setContent(clipboardContent);

        // 弹出toast消息
        ToastMessage.getInstance().popMessage("复制成功", ToastTypes.SUCCESS);
    }

    @FXML
    protected void copyTextFieldDown() {
        Clipboard sc = Clipboard.getSystemClipboard();
        ClipboardContent clipboardContent = new ClipboardContent();
        clipboardContent.put(DataFormat.PLAIN_TEXT, textField32Down.getText());

        sc.setContent(clipboardContent);
        // 弹出toast消息
        ToastMessage.getInstance().popMessage("复制成功", ToastTypes.SUCCESS);
    }

}
